var util = require('../../../utils/util.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      ispand:options.ispand  
    });
    if(options.ispand=='yes'){   //已生成盘点单
      this.pand_detail_fun();
    }else if(options.ispand=='no'){  //还未生成盘点单
      console.log(JSON.stringify(wx.getStorageSync('pand_detail')));
      var goodList=Object.values(wx.getStorageSync('pand_detail'));
      // pand_money合计
      var pand_money = goodList.reduce(function (total, currentValue, currentIndex, arr) {
        return total + (parseFloat(currentValue.pand_num)-parseFloat(currentValue.num))*(currentValue.price)*1;
      }, 0);
      this.setData({ 
        cur_time:util.formatTime(new Date()).substring(5,16),   
        pand_person:app.globalData.loginInfo.real_name?app.globalData.loginInfo.real_name:app.globalData.loginInfo.nickname,    
        goodList:goodList,    
        p_total:wx.getStorageSync('p_total'),
        pand_money:pand_money.toFixed(2)
      });
    }  
  },
  // 盘点单详情接口
  pand_detail_fun:function(){
    var that = this;
    app.AppHttp('api/Order/inventory_order_detail', {
      openid:app.globalData.openid,
      order_id:that.data.id
    }, function (res) {
      console.log(JSON.stringify(res.data));
      var pand_money=0;
      for(var i=0;i<res.data.detail.length;i++){
        pand_money+=(res.data.detail[i].total_money)*1;
      }
      that.setData({
        pand_money:pand_money.toFixed(2),
        order_code:res.data.order_code,
        pand_person:res.data.real_name,
        cur_time:res.data.detail[0].create_time,
        p_total:res.data.detail.length,
        goodList:res.data.detail
      });
    });
  },
  // 清除本地盘点数据
  pand_clear_data:function(){
    wx.showModal({
      title: '提示',
      content: '请确认取消',
      success: function (res) {
        if (res.confirm) {
          wx.removeStorageSync('pand_detail');
          wx.removeStorageSync('p_total');
          wx.removeStorageSync('pand_money');
          wx.navigateBack({
            delta:1
          });
        }
      }
    });   
  },
  // 盘点提交
  pand_submit:function(){
    var pand_list=[];
    for(var i in this.data.goodList){
      console.log(this.data.goodList[i]);
      pand_list.push({
        'good_id':this.data.goodList[i].id,
        'pr_name':this.data.goodList[i].trade_name,
        'unit_name':this.data.goodList[i].unit_name,
        'true_num':this.data.goodList[i].pand_num,
        'strock_num':this.data.goodList[i].num,
        'real_num':this.data.goodList[i].pand_num-this.data.goodList[i].num,
        'price':this.data.goodList[i].price,
        'total':this.data.goodList[i].xji,
        'detail_remark':this.data.goodList[i].detail_remark
      });
    };
    console.log(JSON.stringify(pand_list));
    wx.showModal({
      title: '提示',
      content: '请确认提交',
      success: function (res) {
        if (res.confirm) {
          app.AppHttp('api/Order/ApiFinishInventOrder', {  
            openid: app.globalData.openid,
            dataDeatil:pand_list
          }, function (res) {
            console.log(JSON.stringify(res.data));
            wx.showToast({
              title: res.data.msg
            });
            wx.removeStorageSync('pand_detail');
            wx.removeStorageSync('p_total');
            wx.removeStorageSync('pand_money');
            wx.navigateBack({
              delta:1
            });
          });
        }
      }
    });  
  },
  noneEnoughPeople:function(){
    return;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})